import notification from 'ant-design-vue/es/notification'
const remoteAddress = ''

export default function (queryPayloadObject) {
  const token = localStorage.getItem('token')
  return fetch(remoteAddress + '/api/graphql', {
    method: 'POST',
    body: JSON.stringify({
      variables: queryPayloadObject.variables,
      query: queryPayloadObject.query
    }),
    headers: {
      // 'Policy': 'strict-origin-when-cross-origin',
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    }
  }).then(response => {
    if (response.ok) {
      return response.json()
    } else {
      throw new Error('--Network Error--')
    }
  }).then(json => {
    // dig into two layer
    // tslint:disable-next-line:forin
    if (json.errors) {
      notification.error({
        message: '错误',
        description: JSON.stringify(json.errors)
      })
    }
    for (const data in json) {
      // tslint:disable-next-line:forin
      for (const f in json[data]) {
        return json[data][f]
      }
    }
  })
}
